home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CGAMEJOY.ARJ / JOYSTKMD.SE < prev    next >
Text File  |  1992-01-08  |  20KB  |  411 lines

  1. joystkmd.se      Middle Level Joystick Routines for JSTKRD2.C
  2.                     Preliminary Structured English
  3.  
  4. MODULES:
  5.     Low Level       /* joystklo.se */
  6.  
  7.                 1: detect               : presence through bios or port
  8.                 2: get_valid_axes       : return mask of valid axes
  9.                 3: get_active_axes      : return mask of active axes
  10.                 4: set_active_axes      : set mask of active axes
  11.                 5  button_state         ; current button state
  12.                 6: button_change        : returns bitmask of button changes
  13.                 7: axis_position        : gets position of given axis
  14.  
  15.     Middle Level    /* this module */
  16.  
  17.                 8: initialize           : detect & inititalize joystick globals
  18.                 9: axis_position_change : returns changes in active axes
  19.                10: real_coords_to_rel   : translates axis pos relative to center
  20.                11: get_center_coords    : gets center position of joystick
  21.                12: set_center_coords    : grabs current position as new center
  22.                13: get_min_coords       ; gets minimum position of joystick
  23.                14: set_min_coords       ; grabs current position as new minimum
  24.                15: get_max_coords       ; gets maximum position of joystick
  25.                16: set_max_coords       ; grabs current position as new maximum
  26.                17: get_tolerance        : get tolerance of change to ignore
  27.                18: set_tolerance        : set tolerance of change to ignore
  28.  
  29.     High Level    /* do it yo-sef */
  30.  
  31.                19: create               : grab interrupt 1Ch & set up queue
  32.                20: destroy              : release interrupt 1Ch & queue
  33.                21: get_joystick_event   : get event from queue
  34.                22: push_joystick_event  : push event into queue
  35.                23: interrupt_handler    : timer interrupt 1Ch polling routine
  36.  
  37. PURPOSE:
  38.             To create an event compatible set of routines to read input from
  39.             the game port. (particularly geared to joysticks).
  40.             Based on the file c_joy.c written by Gary Blaine,
  41.             CIS 72707,1736  March 4, 1990 which is available in the
  42.             Borland C Programming Forum on Compuserve in the file JOY.ARC.
  43.             Use assembly for time-critical operations.
  44.  
  45. GLOBAL DEFINES:
  46.          ; return values for button_change and button state
  47.  
  48.             JA1_BUTTON_DOWN        16        ; joystick A, button 1, down
  49.             JA1_BUTTON_UP           1        ; joystick A, button 1, up
  50.             JA2_BUTTON_DOWN        32        ; joystick A, button 2, down
  51.             JA2_BUTTON_UP           2        ; joystick A, button 2, up
  52.  
  53.             JB1_BUTTON_DOWN        64        ; joystick B, button 1, down
  54.             JB1_BUTTON_UP           4        ; joystick B, button 1, up
  55.             JB2_BUTTON_DOWN       128        ; joystick B, button 2, down
  56.             JB2_BUTTON_UP           8        ; joystick B, button 2, up
  57.  
  58.             J_ANY_BUTTON_DOWN    0xf0        ; any button down: either stick
  59.  
  60.             J_A_BUTTON_DOWN      0x30        ; any button down: joystick A
  61.             J_B_BUTTON_DOWN      0xC0        ; any button down: joystick B
  62.  
  63.             J_BUTTON_1_DOWN      0x50        ; button 1 down: either joystick
  64.             J_BUTTON_2_DOWN      0xA0        ; button 2 down: either joystick
  65.  
  66.          ; values for axis_position, axis_position_change & set_active_axes
  67.  
  68.             JAX_AXIS                1        ; x axis mask for joystick A
  69.             JAY_AXIS                2        ; y axis mask for joystick A
  70.             JBX_AXIS                4        ; x axis mask for joystick B
  71.             JBY_AXIS                8        ; y axis mask for joystick B
  72.  
  73.          ; values for functions with the stick parameter
  74.  
  75.             JOYSTICK_A              3        ; axis mask for joystick A
  76.             JOYSTICK_B           0x0C        ; axis mask for joystick B
  77.               
  78.             struct StickValues {
  79.                     int     jax ;       ; joystick A x-coordinate value
  80.                     int     jay ;       ; joystick A y-coordinate value
  81.                     int     jbx ;       ; joystick B x-coordinate value
  82.                     int     jby ;       ; joystick B y-coordinate value
  83.             } ;
  84.             struct JoystickPosition {
  85.                     int     jx  ;       ; joystick x-coordinate value
  86.                     int     jy  ;       ; joystick y-coordinate value
  87.             } ;
  88.            struct Tolerance {
  89.                    int      axl  ;      ; joystick A x-low breakpoint
  90.                    int      axm  ;      ; joystick A x-mid breakpoint
  91.                    int      axh  ;      ; joystick A x-hi  breakpoint
  92.                    int      ayl  ;      ; joystick A y-low breakpoint
  93.                    int      aym  ;      ; joystick A y-mid breakpoint
  94.                    int      ayh  ;      ; joystick A y-hi  breakpoint
  95.                    int      bxl  ;      ; joystick B x-low breakpoint
  96.                    int      bxm  ;      ; joystick B x-mid breakpoint
  97.                    int      bxh  ;      ; joystick B x-hi  breakpoint
  98.                    int      byl  ;      ; joystick B y-low breakpoint
  99.                    int      bym  ;      ; joystick B y-mid breakpoint
  100.                    int      byh  ;      ; joystick B y-hi  breakpoint
  101.                    char     t1   ;      ; low graded tolerance
  102.                    char     t2   ;      ; mid graded tolerance
  103.                    char     t3   ;      ; hi  graded tolerance
  104.            } ;           
  105.  
  106. GLOBAL VARIABLES:
  107.  
  108.             G_Stick_Centers:        coords of joystick centers
  109.             G_Stick_Minimums:       minimum joystick coordinates
  110.             G_Stick_Maximums:       maximum joystick coordinates
  111.             G_Stick_Last_Pos:       joystick coordinates last read
  112.             G_Stick_Tolerance:      ignore change value byte
  113.             G_Stick_Breaks:         breakpoints for joystick tolerance
  114.  
  115. /* not currently used--- */
  116.             G_Stick_Reset_Flag:     reset in progress flag for clock interrupts
  117.  
  118. /*---------------------------------------------------------------------*/
  119. initialize ( void(*NotifyUserFunc)(int) )
  120.                                         : detect & inititalize joystick globals
  121.     parameters: NotifyUserFunc
  122.         ; User Notification function which displays the given prompt (int)
  123.     variables:  retval
  124.     globals:    G_Stick_Centers, G_Stick_Minimums, G_Stick_Maximums,
  125.                 G_Stick_Last_Pos, G_Stick_Tolerance
  126.     BEGIN
  127.         clear G_Stick_Active_Axes
  128.         retval = detect()       ; inst check & set up valid and active axes
  129.         if ( (retval & (JAX_AXIS | JAY_AXIS)) == (JAX_AXIS | JAY_AXIS) )
  130.             G_Stick_Active_Axes = (JAX_AXIS | JAY_AXIS) ;
  131.         if ( (retval & (JBX_AXIS | JBY_AXIS)) == (JBX_AXIS | JBY_AXIS) )
  132.             G_Stick_Active_Axes = G_Stick_Active_Axes | (JBX_AXIS | JBY_AXIS) ;
  133.         if G_Stick_Active_Axes == 0
  134.             call NotifyUserFunc with message 0
  135.             return 0
  136.         call NotifyUserFunc with message 1  (press escape to quit)
  137.         if user_quit = call init_sub (  2, J_ANY_BUTTON_DOWN, &G_Stick_Centers )
  138.             return fail
  139.         if user_quit = call init_sub (  3, J_BUTTON_1_DOWN, &G_Stick_Minimums )
  140.             return fail
  141.         if user_quit = call init_sub (  4, J_BUTTON_2_DOWN, &G_Stick_Maximums )
  142.             return fail
  143.         clear G_Stick_Last_Pos
  144.         if ( G_Stick_Maximums.jax != 0 )
  145.             Jstk = G_Stick_Maximums.jax + G_Stick_Maximums.jay
  146.             G_Stick_Tolerance = G_Stick_Maximums.jax divided by 64
  147.         else if ( G_Stick_Maximums.jbx != 0 )
  148.             Jstk = G_Stick_Maximums.jbx + G_Stick_Maximums.jby
  149.             G_Stick_Tolerance = G_Stick_Maximums.jbx divided by 64
  150.         set_tolerance G_Stick_Tolerance
  151.         return success
  152.     END
  153.                                     
  154. init_sub  ( int msg, button, struct StickValues *what )
  155.     parameters:  msg        ; User Notification message
  156.                  button     ; button to signal position
  157.                  what       ; what global variables to set
  158.     variables:  retval
  159.     BEGIN
  160.         call NotifyUserFunc( msg )
  161.         initialize retval to NULL
  162.         do
  163.             if key waiting in keyboard buffer
  164.                 retval = getkey()
  165.                 if (retval == 27)
  166.                     call NotifyUserFunc with message -1 (user terminate)
  167.                     call NotifyUserFunc with message -2 (warning)
  168.                     return fail
  169.             retval = button_change()
  170.         while ( retval & stop_button == 0 )
  171.         what->jax = axis_position( JAX_AXIS  )
  172.         what->jay = axis_position( JAY_AXIS  )
  173.         what->jbx = axis_position( JBX_AXIS  )
  174.         what->jby = axis_position( JBY_AXIS  )
  175.         return success
  176.     END
  177.  
  178. NotifyUserFunc               ;  Program-specific user display prompts
  179.                              ;  Supplied as a parameter to initialize ()
  180.     parameters: msg
  181.     BEGIN
  182.         display message...
  183.         switch (msg)
  184.             case  -2: "warning: joystick installation incomplete"
  185.             case  -1: "joystick installation terminated by user"
  186.             case   0: "no joystick detected"
  187.             case   1: "press escape to quit"
  188.             case   2: "center joystick and press button"
  189.             case   3: "move joystick to top-left and press button 1"
  190.             case   4: "move joystick to lower-right and press button 2"
  191.             case  -3: return
  192.             default : return
  193.     END                                                                  
  194.  
  195. /*---------------------------------------------------------------------*/
  196. axis_position_change ( int stick, struct JoystickPosition *stickpos )
  197.                                             : returns changes in active axes
  198.     parameters: stick, stickpos
  199.     variables:  x_pos, y_pos
  200.     globals:    G_Stick_Last_Pos, G_Stick_Tolerance
  201.     BEGIN
  202.         if stick is JOYSTICK_A or JOYSTICK_B
  203.         BEGIN
  204.             if stick AND get_active_Axes == stick
  205.             BEGIN
  206.                 if stick == JOYSTICK_A
  207.                 BEGIN
  208.                     x_pos = JstGetPosition( JAX_AXIS )
  209.                         if x_pos < G_Stick_Breaks.axl       ; set x tolerance
  210.                             xtol = G_Stick_Breaks.t1
  211.                         else if x_pos < G_Stick_Breaks.axm
  212.                             xtol = G_Stick_Breaks.t2
  213.                         else if x_pos < G_Stick_Breaks.axh
  214.                             xtol = G_Stick_Breaks.t3
  215.                     y_pos = JstGetPosition( JAY_AXIS )
  216.                         if y_pos < G_Stick_Breaks.ayl       ; set y tolerance
  217.                             ytol = G_Stick_Breaks.t1
  218.                         else if y_pos < G_Stick_Breaks.aym
  219.                             ytol = G_Stick_Breaks.t2
  220.                         else if y_pos < G_Stick_Breaks.ayh
  221.                             ytol = G_Stick_Breaks.t3
  222.                                                         ; check for pos change
  223.                     if ( x_pos < G_Stick_Last_Pos.jax - xtol  or
  224.                          x_pos > G_Stick_Last_Pos.jax + xtol  or
  225.                          y_pos < G_Stick_Last_Pos.jay - ytol  or
  226.                          y_pos > G_Stick_Last_Pos.jay + ytol  )
  227.                     BEGIN
  228.                             G_Stick_Last_Pos.jax = x_pos
  229.                             G_Stick_Last_Pos.jay = y_pos
  230.                             stickpos->jx = x_pos
  231.                             stickpos->jy = y_pos
  232.                             return change: 1
  233.                     END
  234.                 END
  235.                 else if stick == JOYSTICK_B
  236.                 BEGIN
  237.                     x_pos = JstGetPosition( JBX_AXIS )
  238.                         if x_pos < G_Stick_Breaks.bxl       ; set x tolerance
  239.                             xtol = G_Stick_Breaks.t1
  240.                         else if x_pos < G_Stick_Breaks.bxm
  241.                             xtol = G_Stick_Breaks.t2
  242.                         else if x_pos < G_Stick_Breaks.bxh
  243.                             xtol = G_Stick_Breaks.t3
  244.                     y_pos = JstGetPosition( JBY_AXIS )
  245.                         if y_pos < G_Stick_Breaks.byl       ; set y tolerance
  246.                             ytol = G_Stick_Breaks.t1
  247.                         else if y_pos < G_Stick_Breaks.bym
  248.                             ytol = G_Stick_Breaks.t2
  249.                         else if y_pos < G_Stick_Breaks.byh
  250.                             ytol = G_Stick_Breaks.t3
  251.                                                         ; check for pos change
  252.                     if ( x_pos < G_Stick_Last_Pos.jbx - xtol  or
  253.                          x_pos > G_Stick_Last_Pos.jbx + xtol  or
  254.                          y_pos < G_Stick_Last_Pos.jby - ytol  or
  255.                          y_pos > G_Stick_Last_Pos.jby + ytol  )
  256.                     BEGIN
  257.                             G_Stick_Last_Pos.jbx = x_pos
  258.                             G_Stick_Last_Pos.jby = y_pos
  259.                             stickpos->jx = x_pos
  260.                             stickpos->jy = y_pos
  261.                             return change: 1
  262.                     END
  263.                 END
  264.             END
  265.         END
  266.         return error/no change: 0
  267.     END
  268. /*---------------------------------------------------------------------*/
  269. real_coords_to_rel ( int stick, struct JoystickPosition *stickpos )
  270.                                     : translates axis pos relative to center
  271.     parameters: stickpos
  272.     variables:  x_pos, y_pos
  273.     globals:    G_Stick_Centers
  274.     BEGIN
  275.         if stick == JOYSTICK_A
  276.         BEGIN
  277.             stickpos->jx = stickpos->jx - G_Stick_Centers.jax
  278.             if stickpos->jx > 0
  279.                 stickpos->jx = stickpos->jx >> 1
  280.             stickpos->jy = - (stickpos->jy - G_Stick_Centers.jay)
  281.             if stickpos->jy < 0
  282.                 stickpos->jy = stickpos->jy / 2
  283.         END
  284.         else if stick == JOYSTICK_B
  285.         BEGIN
  286.             stickpos->jx = stickpos->jx - G_Stick_Centers.jbx
  287.             if stickpos->jx > 0
  288.                 stickpos->jx = stickpos->jx >> 1
  289.             stickpos->jy = - (stickpos->jy - G_Stick_Centers.jby)
  290.             if stickpos->jy < 0
  291.                 stickpos->jy = stickpos->jy / 2
  292.         END
  293.     END
  294. /*---------------------------------------------------------------------*/
  295. get_min_coords(stick, ptrJoyPos )   ; gets center position for joystick
  296.     BEGIN
  297.         if stick == JOYSTICK_A
  298.             ptrJoyPos->jx = G_Stick_Centers.jax
  299.             ptrJoyPos->jy = G_Stick_Centers.jay
  300.         if stick == JOYSTICK_B
  301.             ptrJoyPos->jx = G_Stick_Centers.jbx
  302.             ptrJoyPos->jy = G_Stick_Centers.jby
  303.     END
  304. /*---------------------------------------------------------------------*/
  305. get_min_coords(stick, ptrJoyPos )   ; gets minimum position for joystick
  306.     BEGIN   
  307.         if stick == JOYSTICK_A
  308.             ptrJoyPos->jx = G_Stick_Minimums.jax
  309.             ptrJoyPos->jy = G_Stick_Minimums.jay
  310.         if stick == JOYSTICK_B
  311.             ptrJoyPos->jx = G_Stick_Minimums.jbx
  312.             ptrJoyPos->jy = G_Stick_Minimums.jby
  313.     END
  314. /*---------------------------------------------------------------------*/
  315. get_max_coords(stick, ptrJoyPos )   : gets maximum position for joystick
  316.     BEGIN   
  317.         if stick == JOYSTICK_A
  318.             ptrJoyPos->jx = G_Stick_Maximums.jax
  319.             ptrJoyPos->jy = G_Stick_Maximums.jay
  320.         if stick == JOYSTICK_B
  321.             ptrJoyPos->jx = G_Stick_Maximums.jbx
  322.             ptrJoyPos->jy = G_Stick_Maximums.jby
  323.     END
  324. /*---------------------------------------------------------------------*/
  325. set_center_coords( stick )          : grabs current position as new center
  326.     BEGIN
  327.         return (call grab_coords( stick, 1 ))
  328.     END
  329. /*---------------------------------------------------------------------*/
  330. set_min_coords( stick )             ; grabs current position as new minimum
  331.     BEGIN
  332.         return (call grab_coords( stick, 2 ))
  333.     END
  334. /*---------------------------------------------------------------------*/
  335. set_max_coords( stick )             ; grabs current position as new maximum
  336.     BEGIN
  337.         return (call grab_coords( stick, 3 ))
  338.     END
  339. /*---------------------------------------------------------------------*/
  340. grab_coords ( int function )
  341.             ; internal subroutine for re_center, set_min_coords, set_max_coords
  342.     parameter:  stick
  343.     variables:  ptrStickValues
  344.     globals:    G_Stick_Centers, G_Stick_Minimums, G_Stick_Maximums
  345.     BEGIN
  346.         if stick is JOYSTICK_A or JOYSTICK_B
  347.         BEGIN
  348.             if stick AND get_active_Axes == stick
  349.             BEGIN
  350.                 if function == 1
  351.                     ptrStickValues = address of G_Stick_Centers
  352.                 if function == 2
  353.                     ptrStickValues = address of G_Stick_Minimums
  354.                 if function == 3
  355.                     ptrStickValues = address of G_Stick_Maximums
  356.                 if stick == JOYSTICK_A
  357.                     ptrStickValues->jax = axis_position( JAX_AXIS  )
  358.                     ptrStickValues->jay = axis_position( JAY_AXIS  )
  359.                 if stick == JOYSTICK_B
  360.                     ptrStickValues->jbx = axis_position( JBX_AXIS  )
  361.                     ptrStickValues->jby = axis_position( JBY_AXIS  )
  362.                 call set_tolerance ( G_Stick_Tolerance )
  363.                 return success
  364.             END
  365.         END
  366.         return error
  367.     END
  368. /*---------------------------------------------------------------------*/
  369. get_tolerance                           : get tolerance of change to ignore
  370.     globals:    G_Stick_Tolerance
  371.     BEGIN
  372.         return  G_Stick_Tolerance
  373.     END
  374. /*---------------------------------------------------------------------*/
  375. set_tolerance (int tolerance)           : set tolerance of change to ignore
  376.     parameters: tolerance
  377.     globals:    G_Stick_Tolerance
  378.     BEGIN
  379.     G_Stick_Tolerance = tolerance ;
  380.     if ( tolerance > 7 )
  381.     {
  382.         G_Stick_Breaks.axl = G_Stick_Centers.jax >> 2 ;
  383.         G_Stick_Breaks.axm = G_Stick_Centers.jax >> 1 ;
  384.         G_Stick_Breaks.axh = G_Stick_Maximums.jax >> 1 ;
  385.         G_Stick_Breaks.ayl = G_Stick_Centers.jay >> 2 ;
  386.         G_Stick_Breaks.aym = G_Stick_Centers.jay >> 1 ;
  387.         G_Stick_Breaks.ayh = G_Stick_Maximums.jay >> 1 ;
  388.         G_Stick_Breaks.bxl = G_Stick_Centers.jbx >> 2 ;
  389.         G_Stick_Breaks.bxm = G_Stick_Centers.jbx >> 1 ;
  390.         G_Stick_Breaks.bxh = G_Stick_Maximums.jbx >> 1 ;
  391.         G_Stick_Breaks.byl = G_Stick_Centers.jby >> 2 ;
  392.         G_Stick_Breaks.bym = G_Stick_Centers.jby >> 1 ;
  393.         G_Stick_Breaks.byh = G_Stick_Maximums.jby >> 1 ;
  394.         G_Stick_Breaks.t1 = (tolerance >> 2) - 1 ;
  395.         G_Stick_Breaks.t2 = tolerance >> 2 ;
  396.         G_Stick_Breaks.t3 = tolerance >> 1 ;
  397.     }
  398.     else
  399.     {
  400.         G_Stick_Breaks.axl = G_Stick_Breaks.axm = G_Stick_Breaks.axh = 0 ;
  401.         G_Stick_Breaks.ayl = G_Stick_Breaks.aym = G_Stick_Breaks.ayh = 0 ;
  402.         G_Stick_Breaks.bxl = G_Stick_Breaks.bxm = G_Stick_Breaks.bxh = 0 ;
  403.         G_Stick_Breaks.byl = G_Stick_Breaks.bym = G_Stick_Breaks.byh = 0 ;
  404.         G_Stick_Breaks.t1 = tolerance ;
  405.         G_Stick_Breaks.t2 = tolerance ;
  406.         G_Stick_Breaks.t3 = tolerance ;
  407.     }
  408.     END
  409.  
  410. /*-- end joystkmd.se ------------------*/
  411.